package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.alibaba.fastjson2.JSONObject;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class WeatherActivity extends AppCompatActivity {
    private TextView tvWeather;
    private TextView tvDay;
    private ImageView imageViewWeather;
    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_weather);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        EventBus.getDefault().register(this);
        tvWeather = findViewById(R.id.tvWeather);
        tvDay = findViewById(R.id.tvDay);
        imageViewWeather=findViewById(R.id.imageViewWeather);


            new Thread(() -> {
                MyHttpConnection httpConnection=new MyHttpConnection();
                String data = httpConnection.doGet("http://t.weather.sojson.com/api/weather/city/101210401");
                JSONObject rootData=JSONObject.parseObject(data);
                JSONObject dataJSONObject = rootData.getJSONObject("data");
                WeatherData weather = JSONObject.parseObject(dataJSONObject.toJSONString(), WeatherData.class);
                EventBus.getDefault().post(new MessageEvent<>(weather));
            }).start();

    }
    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent<WeatherData> event) {
        WeatherData data = event.getData();
        List<Weather> forecast = data.getForecast();
        Weather weather = forecast.get(0);
        tvWeather.setText("今日温度："+data.getWendu()+" ℃");
        tvDay.setText(weather.getType()+"  "+weather.getHigh()+"/"+weather.getLow());
        switch (weather.getType()){
            case "晴":
                imageViewWeather.setImageResource(R.mipmap.sun);
                break;
            case "多云":
                imageViewWeather.setImageResource(R.mipmap.cloud);
                break;
            case "小雨":
                imageViewWeather.setImageResource(R.mipmap.rain);
                break;
            case "阴":
                imageViewWeather.setImageResource(R.mipmap.cloud);
                break;
            default:
                imageViewWeather.setImageResource(R.mipmap.sun);
                break;
        }
    }
}